/**
 * 剑指 Offer 55 - I. 二叉树的深度
 * https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/
 */
const { createTreeNode } = require('../../libs/public');

/**
 * @param {TreeNode} root
 * @return {number}
 */
function maxDepth(root) {
  if (!root) {
    return 0;
  }
  let pre = [root];
  let depth = 0;
  while (pre.length) {
    depth += 1;
    const next = [];
    for (let i = 0; i < pre.length; i += 1) {
      const node = pre[i];
      if (node.left) {
        next.push(node.left);
      }
      if (node.right) {
        next.push(node.right);
      }
    }
    pre = next;
  }
  return depth;
}

console.log(maxDepth(createTreeNode([3, 9, 20, null, null, 15, 7])) === 3);
